package gui.listener;

import gui.panel.BackupPanel;
import gui.panel.ConfigPanel;
import gui.panel.MainPanel;
import service.ConfigService;
import util.MysqlUtil;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class RecoverListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        BackupPanel backupPanel = BackupPanel.instance;
        String mysqlPath = new ConfigService().get(ConfigService.mysqlPath);

        if(0 == mysqlPath.length()) {
            JOptionPane.showMessageDialog(backupPanel,"恢复前请事先配置mysql的路径");
            MainPanel.instance.workingPanel.show(ConfigPanel.instance);
            ConfigPanel.instance.tfMysqlPath.grabFocus();
            return;
        }

        JFileChooser fc = new JFileChooser();
        fc.setSelectedFile(new File("hutubill.sql"));
        fc.setFileFilter(new FileFilter() {
            @Override
            public boolean accept(File f) {
                return f.getName().toLowerCase().endsWith(".sql");
            }

            @Override
            public String getDescription() {
                return ".sql";
            }
        });

        while (true) {
            int returnVal = fc.showOpenDialog(backupPanel);
            File file = fc.getSelectedFile();
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                try {
                    if (file.exists()) {
                        MysqlUtil.recover(mysqlPath, file.getAbsolutePath());
                        JOptionPane.showMessageDialog(backupPanel, "恢复成功");
                        break;
                    } else {
                        JOptionPane.showMessageDialog(backupPanel, "文件不存在，请重新选择");
                    }
                } catch (Exception e1) {
                    e1.printStackTrace();
                    JOptionPane.showMessageDialog(backupPanel, "恢复失败\r\n,错误:\r\n" + e1.getMessage());
                }
            } else if (returnVal == JFileChooser.CANCEL_OPTION){
                break;
            }
        }
    }
}
